package org.book.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.book.dao.BorrowBean;
import org.book.service.BorrowService;

/**
 * Servlet implementation class BorrowDetailsServlet
 */
@WebServlet("/BorrowDetailsServlet")
public class BorrowDetailsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BorrowDetailsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		String textName = request.getParameter("textName");
		String user_id = request.getParameter("user_id");
		Object obj = request.getParameter("pageCurrent");
		int user = 0;
		if(user_id!=null) {
			user = Integer.parseInt(user_id);
		}
		
		BorrowService borrowService = new BorrowService();
		
		List<BorrowBean> list = new ArrayList<>();
		
		int pageCurrent = 1;
		
		if(obj!=null) {
			pageCurrent = Integer.parseInt(obj.toString());
		}
		
		//搜索
		if(textName!=null) {
			list = borrowService.getLike(textName, user);
		}
		//显示
		else {
			list = borrowService.getList(pageCurrent,10,user);
		}
		
		request.setAttribute("list", list);
		
		request.setAttribute("pageCurrent", pageCurrent);
		
		request.setAttribute("count", borrowService.getCount(user));
		
		request.getRequestDispatcher("pages/home/userDetails.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
